var app = getApp();
var bmap = require('../budu-map/bmap-wx.min.js');
var wxMarkerData = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    'address': '定位中',
    ak: "AXMRrsEZ0CGfogyRENeexOTkHxauhZtz",   //填写申请到的ak 
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    brand:[],
    qiang:[],
    spect:[],
    shop:[],
    pro:[],
    clock: '',
    start_time:0,
    intervarID:''
  },

  gotoshop:function (e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../shop_store/shop_store?shopId=' + id,
    })
  },

  gotoProduct:function (e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../product/product?productId='+id,
    })
  },

  // countdown: function () {
  //   var that = this;
  //   var start_time = that.data.start_time;
  //   console.log(start_time)
  //   console.log(new Date().getTime().slice(-3))
  //   if (start_time < new Date().getTime()){
  //     that.setData({
  //       clock:'疯抢中'
  //     });
  //     return false;
  //   }
  //   this.data.intervarID = setInterval(function () {
  //     var leftTime = start_time - (new Date()).valueOf(); //计算剩余的毫秒数 
  //     var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数 
  //     var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
  //     var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟 
  //     var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数 
     
  //     that.setData({
  //       clock: days + "天" + hours + "小时" + minutes + "分" + seconds + "秒"
  //     })
  //     if (days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
  //       clearInterval(that.data.intervarID);

  //       that.setData({
  //         clock: '疯抢中'
  //       })
  //     }
  //   }, 1000
  //   )
  // },

  gotolist: function (e) {
    var bid = e.target.dataset.bid;
    var ptype = e.target.dataset.ptype;
    var name = e.target.dataset.name;
    wx.navigateTo({
      url: '../listdetail/listdetail?bid='+bid+'&ptype='+ptype+'&name='+name,
    })
  },

  search:function () {
    wx.switchTab({
      url: '../category/category',
    })
  },

  qiang:function () {
    wx.navigateTo({
      url: '../listdetail/listdetail?ptype=qiang&name=限时抢购',
    })
  },

  spect: function () {
    wx.navigateTo({
      url: '../listdetail/listdetail?ptype=spect&name=固特异专区',
    })
  },

  shopList:function () {
    wx.navigateTo({
      url: '../shop/shop',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //调用应用实例的方法获取全局数据
    var that = this;
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Index/index',
      method: 'post',
      data: { 
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.status;
        if (status == 1) {
          var pro = res.data.renqi;
          var imgUrls = res.data.ggtop;
          var shop = res.data.shop;
          var spect = res.data.spect;
          var brand = res.data.brand;
          var qiang = res.data.qiang;
          var start_time = res.data.start_time;
          
          that.setData({
            imgUrls: imgUrls,
            pro: pro,
            shop: shop,
            spect: spect,
            brand: brand,
            qiang: qiang,
            start_time: start_time
          });
          // that.countdown();
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        };
        
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });

    /* 获取定位地理位置 */
    // 新建bmap对象   
    var BMap = new bmap.BMapWX({
      ak: that.data.ak,
    });
    var fail = function (data) {
      
    };
    var success = function (data) {
      //返回数据内，已经包含经纬度  
      
      //使用wxMarkerData获取数据  
      wxMarkerData = data.originalData.result.addressComponent.city
      //把所有数据放在初始化data内  
      
      that.setData({
        address: wxMarkerData
      });
    }
    // 发起regeocoding检索请求   
    BMap.regeocoding({
      fail: fail,
      success: success
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})